package cn.com.cifi.mars.web.action.sys;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.vo.sys.rsp.UserRoleVo;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.constant.LogEventEnum;
import cn.com.cifi.mars.constant.LogSrcEnum;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.service.SysUserRoleService;
import cn.com.cifi.mars.util.WebUtils;


@RestController
public class SysUserRoleController extends BaseAction implements ISysUserRoleController{

	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysLogService sysLogService;
	
	/**
	 * 分配用户分页
	 */
	@Override
	public R<UserRoleVo> getUserRolePageList(String apiVersion, String current, String pageSize, String roleId) {
		return ok(sysUserRoleService.queryUserRolePage(current, pageSize, roleId));
	}


	/**
	 * 权限管理-移除用户
	 */
	@Override
	public R<String> deleteUserRole(String apiVersion, String id) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增事件
		SysLogBO bo=new SysLogBO();
		bo.setEvent(LogEventEnum.DELETE);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("权限管理");
		bo.setTitle("权限管理-移除用户");
		bo.setContent("权限管理-移除用户");
		sysLogService.add(bo,user.getAlias());
		return ok(sysUserRoleService.deleteUserRole(id));
	}

	/**
	 * 权限管理-角色用户选择
	 */
	@Override
	public R<String> createUserRole(String apiVersion, List<String> userIds,String roleId) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		// 新增事件
		SysLogBO bo=new SysLogBO();
		bo.setEvent(LogEventEnum.ADD);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("权限管理");
		bo.setTitle("权限管理-角色用户选择");
		bo.setContent("权限管理-角色用户选择");
		sysLogService.add(bo,user.getAlias());
		return ok(sysUserRoleService.createUserRole(userIds,roleId,user.getAlias()));
	}

	/**
	 * 查询用户拥有多少角色
	 */
	@Override
	public R<UserRoleVo> getUserRoleList(String apiVersion, String userId) {
		return ok(sysUserRoleService.getUserRoleList(userId));
	}
}
